package com.sen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sen.dto.BookDTO;
import com.sen.entity.Book;
import com.sen.mapper.BookMapper;
import com.sen.service.BookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sen
 * @since 2021-03-13
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    private BookMapper bookMapper;

    /**
     * 查询所有的图书信息-分页
     */
    @Override
    public List<Book> getBookList(Integer current, Integer size) {
        IPage<Book> page = new Page<>(current, size);
        IPage<Book> bookIPage = bookMapper.selectPage(page, null);
        return bookIPage.getRecords();
    }

    /**
     * 新增书籍
     */
    @Override
    public boolean saveBook(BookDTO bookDTO) {
        Book book = new Book();
        BeanUtils.copyProperties(bookDTO, book);
       return this.save(book);
    }

    /**
     * 更新书籍
     */
    @Override
    public boolean updateBook(BookDTO bookDTO) {
        Book book = new Book();
        BeanUtils.copyProperties(bookDTO, book);
        return this.updateById(book);
    }

    /**
     * 根据书籍id删除书籍
     */
    @Override
    public boolean deleteBookById(Integer id) {
        return this.removeById(id);
    }

    /**
     * 根据类别ID查询书籍
     * @param typeId
     * @return
     */
    @Override
    public List<Book> getBookListByTypeId(Integer typeId) {
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id", typeId);
        List<Book> bookList = bookMapper.selectList(wrapper);
        return bookList;
    }
}
